package com.ian.offer;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author zhangyaowen
 * @date 2021/2/23 9:19 下午
 */
public class MajorityElement {

    public int majorityElement(int[] nums) {

        Map<Integer,Integer> map  =new HashMap<>();

        for (int i = 0; i < nums.length; i++) {

            if ((map.get(nums[i]) ==null?0: map.get(nums[i]))>nums.length>>1){
                return nums[i];
            }

            map.put(nums[i],map.containsKey(nums[i])?map.get(nums[i])+1:1);

        }

        return 0;
    }

    public int majorityElement3(int[] nums) {

        int x = 0 ,v = 0;

        for (int i = 0; i < nums.length; i++) {
             if (v ==0) x =nums[i];
              v += x == nums[i]?1:-1;
        }


        return x;
    }
}
